package com.sxmmall.ware.service.impl;

import com.sxmmall.common.to.SkuHasStockVo;
import com.sxmmall.common.utils.R;

import com.sxmmall.ware.feign.ProductFeignServices;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sxmmall.common.utils.PageUtils;
import com.sxmmall.common.utils.Query;

import com.sxmmall.ware.dao.WareSkuDao;
import com.sxmmall.ware.entity.WareSkuEntity;
import com.sxmmall.ware.service.WareSkuService;


@Service("wareSkuService")
public class WareSkuServiceImpl extends ServiceImpl<WareSkuDao, WareSkuEntity> implements WareSkuService {

    @Autowired
    private ProductFeignServices productFeign;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<WareSkuEntity> query = new QueryWrapper<>();
        String skuId = (String) params.get("skuId");
        if (StringUtils.isNotEmpty(skuId)) {
            query.eq("sku_id", skuId);
        }

        String wareId = (String) params.get("wareId");
        if (StringUtils.isNotEmpty(wareId)) {
            query.eq("ware_id", wareId);
        }

        IPage<WareSkuEntity> page = this.page(
                new Query<WareSkuEntity>().getPage(params),
                query
        );

        return new PageUtils(page);
    }

    @Override
    public void addStock(Long skuId, Long wareId, Integer skuNum) {
        QueryWrapper<WareSkuEntity> query = new QueryWrapper<>();
        query.eq("sku_id", skuId).eq("ware_id", wareId);
        WareSkuEntity one = this.getOne(query);
        if (one != null) {
            this.baseMapper.addStock(skuId, wareId, skuNum);
        } else {
            WareSkuEntity wareSku = new WareSkuEntity();
            wareSku.setSkuId(skuId);
            wareSku.setStockLocked(0);
            wareSku.setStock(skuNum);
            wareSku.setWareId(wareId);
            // 调用远程服务获取sku的名字
            try {
                R info = productFeign.info(skuId);
                if (info.getCode() == 0) {
                    Map<String, Object> skuInfo = (Map<String, Object>) info.get("skuInfo");
                    wareSku.setSkuName(skuInfo.get("skuName").toString());
                }
            } catch (Exception e) {

            }
            this.save(wareSku);
        }
    }

    @Override
    public List<SkuHasStockVo> getSkuHasStock(List<Long> skuIds) {
        return skuIds.stream().map(skuId -> {
            SkuHasStockVo sku = new SkuHasStockVo();
            Long stock = this.baseMapper.getSkuHasStock(skuId);
            sku.setSkuId(skuId);
            sku.setHasStock(stock>0);
            return sku;
        }).collect(Collectors.toList());
    }
}